
from collections import Counter
import jieba
from db_conn.getBy import getByProvince, getByProvinceAndYear
from utils.stop import getStopList

"""
生成词云的数据处理层
"""
def getWordCnt(province,year):
    #getdata
    data=getByProvinceAndYear(province,year)
    if len(data)==0:
        return '0'
    else:
        nameList=[]
        for i in data:
            nameList.append(i[0])
        # print(nameList)
        #分词
        wordList=[]
        for name in nameList:
            word_seg=jieba.cut(name, cut_all=False)
            for word in word_seg:
                wordList.append(word)
        wordCount = Counter(wordList)
        #读入高频无用词和停用词
        stopList=getStopList()
        for i in stopList:
            if wordCount.__contains__(i):
                wordCount.pop(i)
        #前50个高频词
        top_50 = wordCount.most_common(50)
        formatted_data = [{'name': key, 'value': value} for key, value in top_50]
        # print(formatted_data)
        return formatted_data
def getUnitCnt(province,year):
    #getdata
    data=getByProvinceAndYear(province,year)
    if len(data)==0:
        return '0'
    else:
        UnitList=[]
        for i in data:
            Unit=i[2]
            if Unit!=None:
                Unit=Unit.split('、')
                for unit in Unit:
                    UnitList.append(unit)#生成单位链

        wordCount = Counter(UnitList)
        #前50个高频词
        top_50 = wordCount.most_common(50)
        # print(top_50)
        formatted_data = [{'name': key, 'value': value} for key, value in top_50]
        # print(formatted_data)
        return formatted_data
def wordCloudProjectService(province):
    list= {}
    for i in range(2021,2024):
        list[i]=getWordCnt(province,i)
    print(list)
    return list
def wordCloudUnitService(province):
    list= {}
    for i in range(2021,2024):
        list[i]=getUnitCnt(province,i)
    print(list)
    return list








if __name__ == '__main__':
    getWordCnt('河北',2021)